<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ajax</title>
    <script src="js/jquery.js"></script>
</head>

<body>
    <script>

        // request url
        let requestURL = "http://127.0.0.1";

        $(function () {
            // 参数
            const param = {
                page: 1,
                size: 10,
                ids: [1, 2, 3, 4],
                name: "花花世界不必认真",
                time: "2023-09-22",
            }

            // delete
            // del(param);

            // update
            // put(param);

            // insert
            // post(param);

            // select
            // get(param);

            // login
            // login(param);
        });

        // delete
        function del(param) {
            $.ajax({
                method: "DELETE",
                url: requestURL + "/demo/delete",
                data: param,
                traditional: true,
                xhrFields: { withCredentials: true },
                success: function (msg) {
                    console.log(msg);
                }, error: function (err) {
                    let json = JSON.parse(err.responseText);
                    console.error("错误信息：", json);
                    console.error("错误编码：", json.status);
                    console.error("错误消息：", json.message);
                }
            });
        }


        // update
        function put(param) {
            $.ajax({
                method: "PUT",
                url: requestURL + "/demo/put",
                data: param,
                traditional: true,
                xhrFields: { withCredentials: true },
                success: function (msg) {
                    console.log(msg);
                }, error: function (err) {
                    let json = JSON.parse(err.responseText);
                    console.error("错误信息：", json);
                    console.error("错误编码：", json.status);
                    console.error("错误消息：", json.message);
                }
            });
        }


        // post
        function post(param) {
            $.ajax({
                method: "POST",
                url: requestURL + "/demo/post",
                data: JSON.stringify(param),
                contentType: 'application/json;charset=UTF-8',
                traditional: true,
                xhrFields: { withCredentials: true },
                success: function (msg) {
                    console.log(msg);
                }, error: function (err) {
                    let json = JSON.parse(err.responseText);
                    console.error("错误信息：", json);
                    console.error("错误编码：", json.status);
                    console.error("错误消息：", json.message);
                }
            });
        }


        // select
        function get(param) {
            $.ajax({
                method: "GET",
                url: requestURL + "/demo/get",
                data: param,
                traditional: true,
                xhrFields: { withCredentials: true },
                success: function (msg) {
                    console.log(msg);
                }, error: function (err) {
                    let json = JSON.parse(err.responseText);
                    console.error("错误信息：", json);
                    console.error("错误编码：", json.status);
                    console.error("错误消息：", json.message);
                }
            });
        }


        // login
        function login(param) {
            $.ajax({
                method: "POST",
                url: requestURL + "/demo/login",
                data: JSON.stringify(param),
                contentType: 'application/json;charset=UTF-8',
                traditional: true,                      // 支持数组传递
                xhrFields: { withCredentials: true },   // 携带cookie
                async: false,                           // false为同步，默认为异步
                success: function (msg) {
                    console.log(msg);
                }, error: function (err) {
                    let json = JSON.parse(err.responseText);
                    console.error("错误信息：", json);
                    console.error("错误编码：", json.status);
                    console.error("错误消息：", json.message);
                }
            });
        }

    </script>
</body>

</html>